import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../colors_util.dart';

showDropDown({
  required GlobalKey key,
  required List<UsuallyModel> list,
  required Function(int) onTap,
}) {
  var robj = key.currentContext?.findRenderObject() as RenderBox?;
  if (robj == null) {
    return;
  }
  Get.log(robj.size.height.toString());
  var offset = robj.localToGlobal(Offset(robj.size.width, robj.size.height));
  var _right = Get.width - offset.dx;
  var _top = offset.dy;
  var _bottom = 0.0;
  if (offset.dy > Get.height / 3 * 2) {
    _bottom = Get.height - _top + robj.size.height;
    _top = 0;
  }
  Get.dialog(
    Column(
      crossAxisAlignment: CrossAxisAlignment.end,
      verticalDirection:
          _top == 0 ? VerticalDirection.up : VerticalDirection.down,
      children: [
        Container(
          margin: EdgeInsets.only(top: _top, right: _right),
          width: robj.size.width,
          height: Get.height - _top,
          constraints: BoxConstraints(
              maxHeight: _top == 0 ? offset.dy : Get.height - _top,
              minHeight: 50.r),
          decoration: BoxDecoration(
              color: Colors.black.withOpacity(0.4),
              borderRadius: BorderRadius.circular(4.r)),
          child: Container(
            child: ListView(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              children: list
                  .asMap()
                  .map(
                    (key, e) => MapEntry(
                      key,
                      GestureDetector(
                        onTap: () {
                          if (Get.isDialogOpen == true) {
                            Get.back();
                          }

                          onTap(key);
                        },
                        child: Container(
                          color: Colors.white,
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 12.r, vertical: 10.r),
                            // height: 50.r,
                            constraints: BoxConstraints(minHeight: 50.r),
                            decoration: UnderlineTabIndicator(
                                insets: EdgeInsets.symmetric(horizontal: 12.r),
                                borderSide: const BorderSide(
                                    width: 1, color: Color(0xffeeeeee))),
                            child: Row(
                              children: [
                                Visibility(
                                  child: e.iconData != null
                                      ? Icon(
                                          e.iconData,
                                          size: 24.r,
                                        )
                                      : e.url != null
                                          ? Image.asset(e.url!,
                                              width: 24.r,
                                              height: 24.r,
                                              fit: BoxFit.contain)
                                          : const SizedBox(),
                                ),
                                Expanded(
                                    child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Flexible(
                                      child: Text(
                                        e.name,
                                        style: TextStyle(
                                            color: e.isSelected
                                                ? ColorsUtil.blue
                                                : const Color(0xff333333),
                                            fontSize: 15.sp),
                                      ),
                                    ),
                                    Visibility(
                                        visible: e.isSelected,
                                        child: Icon(
                                          Icons.check,
                                          size: 20.r,
                                          color: ColorsUtil.blue,
                                        ))
                                  ],
                                ))
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  )
                  .values
                  .toList(),
            ),
          ),
        ),
      ],
    ),
    barrierColor: Colors.transparent,
    useSafeArea: false,
    barrierDismissible: true,
  );
}

class UsuallyModel {
  String? url;
  IconData? iconData;
  String name;
  int? unReadCount;
  bool isSelected;
  VoidCallback? onTap;
  UsuallyModel(
      {this.url,
      this.iconData,
      required this.name,
      this.unReadCount,
      this.isSelected = false,
      this.onTap});
}
